package com.bskj.bms.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.bskj.bms.bean.req.PasswordReqVo;
import com.bskj.bms.service.IBmsIndexService;
import com.bskj.bms.service.IBmsUserService;
import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.enums.UserStatusEnum;
import com.bskj.common.utils.CodeUtils;
import com.bskj.common.utils.StringUtils;

@Controller
@RequestMapping("index")
public class IndexController extends BaseController {
	@Autowired
	private IBmsUserService bmsUserService;

	@Autowired
	private IBmsIndexService bmsIndexService;

	@RequestMapping
	public ModelAndView index(HttpServletRequest req, String errorMsg) throws Exception {
		ModelAndView mv = new ModelAndView();
		BmsUser bmsUser = getSessionUser();
		if (bmsUser == null) {
			mv.setViewName("index/login");
			mv.addObject("errorMsg", errorMsg);
		} else {
			mv.setViewName("/index/index");
			mv.addObject("user", getSession("user"));
			mv.addObject("roleName", getSession("roleName"));
			mv.addObject("menuTree", getSession("menuTree"));
			mv.addObject("parkName", getSession("parkName"));
		}
		return mv;
	}

	// @RequestMapping("/getCode")
	// public void getCode(HttpServletRequest req, HttpServletResponse resp)
	// throws Exception {
	// CodeUtils.getCode(req, resp);
	// }

	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest req, String username, String password) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("redirect:/index");
		if (StringUtils.isBank(username) || StringUtils.isBank(password)) {
			mv.addObject("errorMsg", "请输入用户名和密码!");
		} else {
			BmsUser bmsUser = bmsUserService.findUser(username, password);
			if (bmsUser == null) {
				mv.addObject("errorMsg", "用户名/密码错误!");
			} else if (bmsUser.getStatus() == UserStatusEnum.disable.getKey()) {
				mv.addObject("errorMsg", "账号已经被禁用，请联系管理员!");
			} else {
				Map<String, Object> resultMap = bmsUserService.loadBmsUserRoleResource(bmsUser);
				setCookieUser(resultMap);
			}
		}
		return mv;
	}

	@RequestMapping("/toModify")
	public ModelAndView toModify() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index/modify");
		return mv;
	}

	@ResponseBody
	@RequestMapping("/modify")
	public Map<String, Object> modify(PasswordReqVo pwd) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		System.out.println(getSessionUser().getId());
		System.out.println(pwd.getNewPwds());
		resultMap = bmsIndexService.modify(pwd.getOldPwd(), pwd.getNewPwd(), pwd.getNewPwds(),
				getSessionUser().getId());
		return resultMap;
	}

	@RequestMapping("/exit")
	public ModelAndView exit() {
		ModelAndView mv = new ModelAndView();
		removeAllSession();
		mv.setViewName("index/login");
		return mv;
	}

	@RequestMapping("/welcome")
	public ModelAndView welcome() {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("index/welcome");
		return mv;
	}
}
